<?php // 
namespace UFSW\UFSWSecurityBundle\Listener;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginListener 
{ 
    private $router,$usuario = null;
    
    public function __construct(Router $router) 
    { 
        $this->router = $router;
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $token = $event->getAuthenticationToken(); 
        $this->usuario = $token->getUser();
    }
    
    public function onKernelResponse(FilterResponseEvent $event) 
    { 
        if (null != $this->usuario) {
            $portada = $this->router->generate('usuario_portada', array(
                'usuario' => $this->usuario->getSlug() 
                )); 
            $event->setResponse(new RedirectResponse($portada));
        } 
    }

}